package com.example.excel.ui.activity.login;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.example.excel.R;
import com.example.excel.base.BaseActivity;
import com.example.excel.config.Constants;
import com.example.excel.databinding.ActivityLoginBinding;
import com.example.excel.http.HttpManager;
import com.example.excel.ui.activity.MainActivity;
import com.example.excel.utils.SharedPreferencesUtils;
import com.example.excel.utils.Utils;

import androidx.lifecycle.Observer;

public class LoginActivity extends BaseActivity<ActivityLoginBinding, LoginViewModel> {

    private boolean isFinish;

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, LoginActivity.class);
        intent.putExtra("isFinish", true);
        activity.startActivity(intent);
    }

    public static void startFinish(Activity activity) {
        Intent intent = new Intent(activity, LoginActivity.class);
        intent.putExtra("isFinish", false);
        activity.startActivity(intent);
        activity.finish();
    }

    @Override
    protected Class<LoginViewModel> initViewModel() {
        return LoginViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void initView() {
        isFinish = getIntent().getBooleanExtra("isFinish", false);
        String userName = SharedPreferencesUtils.getString(Constants.SP_USER_NAME, "");
        if (!TextUtils.isEmpty(userName)) {
            mBinding.etPhone.setText(userName);
        }
        String userPass = SharedPreferencesUtils.getString(Constants.SP_USER_PASS, "");
        if (!TextUtils.isEmpty(userPass)) {
            mBinding.etPassword.setText(userPass);
        }
        String string = SharedPreferencesUtils.getString(Constants.SP_BASE_URL_HTTP, "");
        String stringPort = SharedPreferencesUtils.getString(Constants.SP_BASE_URL_HTTP_PORT, "");
        if (TextUtils.isEmpty(string)) {
            string = "24.86.111.11";
            stringPort = "8090";
            SharedPreferencesUtils.setString(Constants.SP_BASE_URL_HTTP, string);
            SharedPreferencesUtils.setString(Constants.SP_BASE_URL_HTTP_PORT, stringPort);
        }
        mBinding.etCode.setText(string);
        mBinding.etCodePort.setText(stringPort);
        mBinding.etPhone.setText("pda01");
        mBinding.etPassword.setText("123456");
        mBinding.btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRefreshBaseUrl();
            }
        });
    }

    @Override
    protected void registerObserve() {
        super.registerObserve();
        mViewModel.loginSuccess.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (aBoolean) {
                    if (isFinish) {
                        finish();
                    } else {
                        MainActivity.start(LoginActivity.this);
                    }
                }
            }
        });
    }

    public void login(View view) {
        String userName = mBinding.etPhone.getText().toString().trim();
        String passWord = mBinding.etPassword.getText().toString().trim();
        if (TextUtils.isEmpty(userName)) {
            Utils.getInstance().showToast(R.string.login_phone_hint);
            return;
        }
        if (TextUtils.isEmpty(passWord)) {
            Utils.getInstance().showToast(R.string.login_password_hint);
            return;
        }
        mViewModel.login(userName, passWord);
    }

    public void onRefreshBaseUrl() {
        String http = mBinding.etCode.getText().toString().trim();
        String httpPort = mBinding.etCodePort.getText().toString().trim();
        if (!TextUtils.isEmpty(http) && !TextUtils.isEmpty(httpPort)) {
            SharedPreferencesUtils.setString(Constants.SP_BASE_URL_HTTP, http);
            SharedPreferencesUtils.setString(Constants.SP_BASE_URL_HTTP_PORT, httpPort);
            HttpManager.getInstance().init("http://" + http + ":" + httpPort + "/");
            Utils.getInstance().showToast("修改成功");
        } else {
            Utils.getInstance().showToast("请输入IP地址或端口号");
        }
    }
}
